
//  YSEMSApplyTripViewController.m
//  YaSha-iOS
//
//  Created by 方鹏俊 on 2017/11/8.
//

#import "YSEMSApplyTripViewController.h"
#import "YSFormRowModel.h"
#import "YSContactModel.h"
#import "YSFormCommonCell.h"
#import "YSChoosePeopleVC.h"
#import "YSDingDingHeader.h"
#import "YSInternalModel.h"
#import "YSPersonalInformationModel.h"
#import "YSEMSAddTripViewController.h"
#import "YSEMSProListViewController.h"
#import "YSEMSProPageController.h"
#import "YSFormDatePickerCell.h"
#import "YSFormSwitchCell.h"
#import "YSContactSelectPersonViewController.h"
//现在可读性太差，没时间改。cell里面也不好动。。。,用model来记录两个页面的数据还有cell的数据，现在是rac传过来的，也可以不传在cell里面记录
@interface YSEMSApplyTripViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *configArray;
////通知传过去的内容
//NSDictionary *trip = @{@"rowModelArray": rowModelArray,
//@"payload": self.payload,
//@"buyTickets": [self.payload[@"buyTickets"] isEqual:@""] ? @"0" : @"1"};
//里面trip字典，一个行程一个字典
@property (nonatomic, strong) NSMutableArray *tripArray;
@property (nonatomic, strong) NSArray *payloadArray;
/**上传参数*/
@property (nonatomic, strong) NSMutableDictionary *payload;
@property (nonatomic, strong) NSMutableArray *tripListArray;
/**添加一个想成将是否车票代买添加进去，然后遍历只要有一个行程需要火车票代买，就必须填写身份证*/
@property (nonatomic, strong) NSMutableArray *buyTicketsArray;
@property (nonatomic, assign) BOOL needIdCard;
@property (nonatomic, assign) BOOL isProMember;

@property (nonatomic, strong) YSPersonalInformationModel *personalInformationModel;

@property (nonatomic, strong) YSEMSAddTripViewController *addTripViewController;

@end

@implementation YSEMSApplyTripViewController

static NSString *cellIdentifier = @"FormCommonCell";
- (YSEMSAddTripViewController *)addTripViewController {
    if (!_addTripViewController) {
        _addTripViewController = [[YSEMSAddTripViewController alloc]init];
    }
    return _addTripViewController;
}
- (NSMutableArray *)configArray {
    if (!_configArray) {
        _configArray = [NSMutableArray array];
        NSMutableArray *section0Array = [NSMutableArray array];
        YSFormRowModel *model0 = [[YSFormRowModel alloc] init];
        model0.rowName = @"YSFormDetailCell";
        model0.necessary = YES;
        model0.title = @"出差人";//点击cell跳转选人
        model0.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        YSFormRowModel *model1 = [[YSFormRowModel alloc] init];
        model1.rowName = @"YSFormDetailCell";
        model1.necessary = YES;
        model1.title = @"所属公司";

        YSFormRowModel *model2 = [[YSFormRowModel alloc] init];
        model2.rowName = @"YSFormTextFieldCell";
        model2.necessary = YES;
        model2.title = @"出差事由";
        //model2.detailTitle = self.payload[@"remark"];
        model2.placeholder = @"请输入";
        
        YSFormRowModel *model3 = [[YSFormRowModel alloc] init];
        model3.rowName = @"YSFormOptionsCell";
        model3.necessary = YES;
        model3.title = @"职务级别";
        
        YSFormRowModel *model4 = [[YSFormRowModel alloc] init];
        model4.rowName = @"YSFormDatePickerCell";
        model4.necessary = YES;
        model4.title = @"出发日期";
        //model4.detailTitle = self.payload[@"startTime"];
        model4.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        model4.datePickerMode = PGDatePickerModeDate;
        
        YSFormRowModel *model5 = [[YSFormRowModel alloc] init];
        model5.rowName = @"YSFormDatePickerCell";
        model5.necessary = YES;
        model5.title = @"返程日期";
        //model5.detailTitle = self.payload[@"endTime"];
        model5.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        model5.datePickerMode = PGDatePickerModeDate;
        
        YSFormRowModel *model6 = [[YSFormRowModel alloc] init];
        model6.rowName = @"YSFormTextFieldCell";
        model6.title = @"身份证号";
        //model6.detailTitle = self.payload[@"idCard"];
        model6.placeholder = @"请输入";
        model6.checkoutType = YSCheckoutID;
        
        YSFormRowModel *model7 = [[YSFormRowModel alloc] init];
        model7.rowName = @"YSFormSwitchCell";
        model7.title = @"是否项目人员";
        //model7.detailTitle = self.payload[@"idCard"];
        
        [section0Array addObjectsFromArray:@[model0, model1, model2, model3, model4,model5, model6, model7]];
        [_configArray addObjectsFromArray:@[section0Array]];
    }
    return _configArray;
}

- (NSMutableArray *)tripArray {
    if (!_tripArray) {
        _tripArray = [NSMutableArray array];
    }
    return _tripArray;
}

- (NSArray *)payloadArray {
    if (!_payloadArray) {
        _payloadArray = @[@"businessPname", @"businessPname", @"remark", @"jobLevelName", @"startTime", @"endTime", @"idCard", @"proPerson"];
    }
    return _payloadArray;
}

- (NSMutableDictionary *)payload {
    if (!_payload) {
        _payload = [NSMutableDictionary dictionary];
        [_payload setValue:self.tripListArray forKey:@"businessTripList"];
        
        [_payload setValue:@"" forKey:@"businessPno"];
        [_payload setValue:@"" forKey:@"businessPname"];
        [_payload setValue:@"" forKey:@"remark"];
        [_payload setValue:@"" forKey:@"jobLevelName"];
        [_payload setValue:@"0" forKey:@"areaCompany"];
        [_payload setValue:@"" forKey:@"startTime"];
        [_payload setValue:@"" forKey:@"endTime"];
        [_payload setValue:@"" forKey:@"idCard"];
        [_payload setValue:@"0" forKey:@"proPerson"];
        [_payload setValue:@"" forKey:@"proName"];
        [_payload setValue:@"" forKey:@"proManagerName"];
    }
    return _payload;
}

- (NSMutableArray *)tripListArray {
    if (!_tripListArray) {
        _tripListArray = [NSMutableArray array];
    }
    return _tripListArray;
}

- (NSMutableArray *)buyTicketsArray {
    if (!_buyTicketsArray) {
        _buyTicketsArray = [NSMutableArray array];
    }
    return _buyTicketsArray;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectedPro:) name:@"EMSSelectedPro" object:nil];
}
- (void)initSubviews {
    [super initSubviews];
    self.title = @"出差申请";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kTopHeight, kSCREEN_WIDTH, kSCREEN_HEIGHT - kTopHeight - kBottomHeight) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[YSFormCommonCell class] forCellReuseIdentifier:cellIdentifier];
    [self.view addSubview:self.tableView];
    UIView *backView = [UIView new];
    backView.backgroundColor = kGrayColor(239);
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-kBottomHeight);
        make.height.mas_equalTo(60*kHeightScale);
    }];
    QMUIButton *addTripButton = [YSUIHelper generateDarkFilledButton];
    [addTripButton setTitle:@"添加行程" forState:UIControlStateNormal];
#pragma mark 添加行程
    YSWeak;
    [[addTripButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        YSStrong;
        /** 检验数据完整性 */
        if ([strongSelf.configArray[0] count] > 8) {//选择了项目
            if ([strongSelf.payload[@"businessPno"] isEqual:@""] || [strongSelf.payload[@"remark"] isEqual:@""] ||             [strongSelf.payload[@"jobLevelName"] isEqual:@""] ||    [strongSelf.payload[@"startTime"] isEqual:@""] || [strongSelf.payload[@"endTime"] isEqual:@""] || [strongSelf.payload[@"proName"] isEqual:@""]) {
                [QMUITips showError:@"请填写完整" inView:strongSelf.view hideAfterDelay:1];
                return;
            }
             [[NSNotificationCenter defaultCenter] removeObserver:strongSelf name:@"EMSSelectedPro" object:nil];
//            YSEMSAddTripViewController *EMSAddTripViewController = [[YSEMSAddTripViewController alloc] init];
//            EMSAddTripViewController.emsArr = self.configArray;
//            EMSAddTripViewController.proListModel = [self.configArray[0][8] proListModel];
//            [self.navigationController pushViewController:EMSAddTripViewController animated:YES];
           
           
            strongSelf.addTripViewController.emsArr = strongSelf.configArray;
            strongSelf.addTripViewController.proListModel = [strongSelf.configArray[0][8] proListModel];
            strongSelf.addTripViewController.configArray = nil;
            strongSelf.addTripViewController.isEditing = NO;
            strongSelf.addTripViewController.editingIndexPath = nil;
            [strongSelf.navigationController pushViewController:strongSelf.addTripViewController animated:YES];
        } else {//未选择项目
            if ([strongSelf.payload[@"businessPno"] isEqual:@""] || [strongSelf.payload[@"remark"] isEqual:@""] || [strongSelf.payload[@"jobLevelName"] isEqual:@""] || [strongSelf.payload[@"startTime"] isEqual:@""] || [strongSelf.payload[@"endTime"] isEqual:@""]) {
                [QMUITips showError:@"请填写完整" inView:strongSelf.view hideAfterDelay:1];
                return;
            }
            [[NSNotificationCenter defaultCenter] removeObserver:strongSelf name:@"EMSSelectedPro" object:nil];
//            YSEMSAddTripViewController *EMSAddTripViewController = [[YSEMSAddTripViewController alloc] init];
//          EMSAddTripViewController.emsArr = self.configArray;
//            [self.navigationController pushViewController:EMSAddTripViewController animated:YES];
           
            strongSelf.addTripViewController.emsArr = strongSelf.configArray;
            strongSelf.addTripViewController.configArray = nil;
            strongSelf.addTripViewController.isEditing = NO;
            strongSelf.addTripViewController.editingIndexPath = nil;
            strongSelf.addTripViewController.proListModel = nil;
            [strongSelf.navigationController pushViewController:strongSelf.addTripViewController animated:YES];
        }
        
    }];
    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.96 alpha:1.00];
    [backView addSubview:addTripButton];
    [addTripButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(backView.mas_centerX);
        make.bottom.mas_equalTo(backView.mas_bottom).offset(-5*kHeightScale);
        make.size.mas_equalTo(CGSizeMake(300*kWidthScale, 50*kHeightScale));
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectPerson:) name:KNotificationPostSelectedPerson object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addTrip:) name:@"AddTripNotification" object:nil];
    [self getUserInfo];
}

- (void)setNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated {
    [super setNavigationItemsIsInEditMode:isInEditMode animated:animated];
    self.navigationItem.rightBarButtonItem = [QMUINavigationButton barButtonItemWithType:QMUINavigationButtonTypeNormal title:@"提交" position:QMUINavigationButtonPositionRight target:self action:@selector(submit)];
    
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.configArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *rowArray = self.configArray[section];
    return rowArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YSFormRowModel *cellModel = self.configArray[indexPath.section][indexPath.row];
    YSFormCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell = [[NSClassFromString(cellModel.rowName) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    [cell setCellModel:cellModel];
    YSWeak;
    __weak __typeof(cell) weakCell = cell;
    [cell.sendValueSubject subscribeNext:^(id x) {
        YSStrong;
        [strongSelf.payload setValue:x forKey:strongSelf.payloadArray[indexPath.row]];//记录上传参数
        
        weakCell.cellModel.detailTitle = x;//有改变model记录下，不用rac也可以在cell里面记录现在放在控制器记录
        
        // 控制时间范围
        if (indexPath.row == 4) {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
            [formatter setDateFormat:@"yyyy-MM-dd"];
            YSFormRowModel *model5 = strongSelf.configArray[0][5];//返程日期
            model5.minimumDate = [formatter dateFromString:strongSelf.payload[@"startTime"]];
        }
        // 控制时间范围
        if (indexPath.row == 5) {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
            [formatter setDateFormat:@"yyyy-MM-dd"];
            YSFormRowModel *model4 = strongSelf.configArray[0][4];
            model4.maximumDate = [formatter dateFromString:strongSelf.payload[@"endTime"]];
        }
        
        
        if ([weakCell isKindOfClass:[YSFormSwitchCell class]] && [x isEqual:@"1"]) {//是否项目人员
            YSFormRowModel *model7 = strongSelf.configArray[0][7];
            model7.switchStatus = YES;
            strongSelf.isProMember = YES;
            YSFormRowModel *model8 = [[YSFormRowModel alloc] init];
            model8.rowName = @"YSFormJumpCell";
            model8.necessary = YES;
            model8.title = @"工程项目名称";
            model8.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            model8.jumpClass = @"YSEMSProPageController";
           
            
            YSFormRowModel *model9 = [[YSFormRowModel alloc] init];
            model9.rowName = @"YSFormDetailCell";
            model9.necessary = YES;
            model9.title = @"项目经理";
          
            [strongSelf.tableView beginUpdates];
            [strongSelf.configArray[0] addObjectsFromArray:@[model8, model9]];
            [strongSelf.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:8 inSection:0], [NSIndexPath indexPathForRow:9 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
            [strongSelf.tableView endUpdates];
        }
        
        if ([weakCell isKindOfClass:[YSFormSwitchCell class]] && [x isEqual:@"0"]) {
            strongSelf.isProMember = NO;
            YSFormRowModel *model7 = strongSelf.configArray[0][7];
            model7.switchStatus = NO;
            [strongSelf.configArray[0] removeObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(8, 9)]];
            [strongSelf.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:8 inSection:0], [NSIndexPath indexPathForRow:9 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }];
    
    //编辑行程  信号订阅
    [weakCell.sendEditSectionSubject subscribeNext:^(YSFormRowModel *rowModel) {
        YSStrong;
        [[NSNotificationCenter defaultCenter] removeObserver:strongSelf name:@"EMSSelectedPro" object:nil];
//        YSEMSAddTripViewController *EMSAddTripViewController = [[YSEMSAddTripViewController alloc] init];
        YSEMSAddTripViewController *EMSAddTripViewController = strongSelf.addTripViewController;
        NSIndexPath *indexPath = [strongSelf.tableView indexPathForCell:weakCell];
        EMSAddTripViewController.isEditing = YES;
        EMSAddTripViewController.editingIndexPath = indexPath;
        EMSAddTripViewController.emsArr = strongSelf.configArray;
        
//        EMSAddTripViewController.configArray = [self.configArray[indexPath.section] mutableCopy];//深拷贝元素地址没变
        NSMutableArray *copyArr = [NSMutableArray array];
        for (YSFormRowModel *model in  strongSelf.configArray[indexPath.section]) {
            [copyArr addObject:[model mutableCopy]];
        }
        [copyArr removeObjectAtIndex:0];//行程头移除
        EMSAddTripViewController.configArray = copyArr;
         if ([strongSelf.configArray[0] count] > 8) {//选择了项目)
             EMSAddTripViewController.proListModel = [strongSelf.configArray[0][8] proListModel];
         }else{
             EMSAddTripViewController.proListModel = nil;
         }
        
        [strongSelf.navigationController pushViewController:EMSAddTripViewController animated:YES];
    }];
    [weakCell.sendDeleteSectionSubject subscribeNext:^(YSFormRowModel *rowModel) {
        YSStrong;
         NSIndexPath *indexPath = [strongSelf.tableView indexPathForCell:weakCell];
        [strongSelf.configArray removeObjectAtIndex:indexPath.section];
         [strongSelf.tripListArray removeObjectAtIndex:indexPath.section -1];
        [strongSelf.buyTicketsArray removeObjectAtIndex:indexPath.section -1];
        //删除时要改变行程数字,遍历刷新整个数组
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.numberStyle = kCFNumberFormatterRoundHalfDown;
       
        for (int i = 0; i < strongSelf.configArray.count; i++) {
            if (i == 0) {//出差信息
            }else{//行程信息,添加行程数字
                YSFormRowModel *model = strongSelf.configArray[i][0];
                    model.title = [NSString stringWithFormat:@"行程%@", [formatter stringFromNumber:[NSNumber numberWithInt:i]]];
            }
        }
        [strongSelf.tableView reloadData];
       
        [strongSelf checkNeedIdCard:strongSelf.buyTicketsArray];
        
    }];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return section == self.configArray.count - 1 ? 110*kHeightScale : 30.0;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                YSContactSelectPersonViewController *contactSelectPersonViewController = [[YSContactSelectPersonViewController alloc]init];
                contactSelectPersonViewController.jumpSourceStr = @"EMS";
                [self.navigationController pushViewController:contactSelectPersonViewController animated:YES];
                break;
            }
            case 9:
            {
                break;
            }
                
        }
    }
}

#pragma mark - 选择完项目回调
- (void)selectedPro:(NSNotification *)notification {
    
    UIViewController *vc =  [YSUtility getCurrentViewController];
    UIViewController *otherVC  = [vc.navigationController.viewControllers objectAtIndex:vc.navigationController.viewControllers.count - 2];
    if (![otherVC.title isEqualToString:@"出差申请"]) {
        return;
    }
    
    
    YSEMSProListModel *cellModel = notification.userInfo[@"proModel"];
    [self.payload setValue:cellModel.name forKey:@"proName"];
    [self.payload setValue:cellModel.proManName forKey:@"proManagerName"];
    [self.payload setValue:cellModel.id forKey:@"proId"];
    [self.payload setValue:cellModel.proNatureCode forKey:@"proCode"];
    [self.payload setValue:cellModel.proManId forKey:@"proManagerId"];
    [self.payload setValue:cellModel.proCompId forKey:@"proCompId"];
    
    YSFormRowModel *model8 = nil;
    YSFormRowModel *model9 = nil;
    if ([self.configArray[0] count] > 9) {
        model8 = self.configArray[0][8];
        model9 = self.configArray[0][9];
        model8.detailTitle = cellModel.name;
        model8.proListModel = cellModel;
        model9.detailTitle = cellModel.proManName;
        [self.tableView reloadData];
//        [self.tableView beginUpdates];
//        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:8 inSection:0], [NSIndexPath indexPathForRow:9 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
//        [self.tableView endUpdates];
    }
    
    
}


#pragma mark - /** 获取默认申请人信息 */
- (void)getUserInfo {
    NSString *url = [NSString stringWithFormat:@"%@%@/%@", YSDomain, getPersonInfo, [YSUtility getUID]];
    [YSNetManager ys_request_GETWithUrlString:url isNeedCache:NO parameters:nil successBlock:^(id response) {
        if ([response[@"code"] isEqual:@1]) {
            _personalInformationModel = [YSPersonalInformationModel yy_modelWithDictionary:response[@"data"]];
            [self.payload setValue:_personalInformationModel.no forKey:@"businessPno"];
            [self.payload setValue:_personalInformationModel.companyName forKey:@"businessPdeptName"];
             [self.payload setObject:_personalInformationModel.name forKey:@"businessPname"];
            
            YSFormRowModel *model0 = _configArray[0][0];
            model0.detailTitle = _personalInformationModel.name;
            NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
            YSFormCommonCell *cell0 = [self.tableView cellForRowAtIndexPath:indexPath0];
            [cell0 updateData:model0];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath0] withRowAnimation:UITableViewRowAnimationAutomatic];
            
            YSFormRowModel *model1 = _configArray[0][1];
            model1.detailTitle = _personalInformationModel.companyName;
            NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
            YSFormCommonCell *cell1 = [self.tableView cellForRowAtIndexPath:indexPath1];
            [cell1 updateData:model1];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath1] withRowAnimation:UITableViewRowAnimationAutomatic];
            //更新职务
            
            //职务不需要从列表选择了，从选择的工号去请求回来
            [YSNetManager ys_request_GETWithUrlString:[NSString stringWithFormat:@"%@%@/%@",YSDomain,getPostNameByNo,_personalInformationModel.no] isNeedCache:NO parameters:nil successBlock:^(id response) {
                DLog(@"-------%@",response);
                if ([response[@"code"] integerValue]) {
                    YSFormRowModel *model3 = _configArray[0][3];
                    model3.detailTitle = response[@"data"][@"postName"];
                    NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0];
                    YSFormCommonCell *cell3 = [self.tableView cellForRowAtIndexPath:indexPath3];
                    [cell3 updateData:model3];
                    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath3] withRowAnimation:UITableViewRowAnimationAutomatic];
                    //配置参数
                    [self.payload setValue:response[@"data"][@"postCode"] forKey:@"jobLevelId"];
                    [self.payload setValue:response[@"data"][@"postName"] forKey:@"jobLevelName"];
                    
                }
            } failureBlock:^(NSError *error) {
                
            } progress:nil];
        }
    } failureBlock:^(NSError *error) {
        DLog(@"error:%@", error);
    } progress:nil];
}
#pragma mark - 选择联系人通知
- (void)selectPerson:(NSNotification *)notification {
    YSContactModel *internalModel = notification.userInfo[@"selectedArray"][0];
//    [self.payload setObject:internalModel.name forKey:@"businessPname"];
//    [self.payload setObject:internalModel.companyId forKey:@"businessId"];
    [self.payload setObject:internalModel.name forKey:@"businessPname"];
    [self.payload setObject:internalModel.userId forKey:@"businessPno"];
//    [self.payload setObject:internalModel.position forKey:@"businessPstation"];
//    [self.payload setObject:internalModel.deptId forKey:@"businessPdeptId"];
//    [self.payload setObject:internalModel.deptName forKey:@"businessPdeptName"];
    //出差人
    YSFormRowModel *model0 = _configArray[0][0];
    model0.detailTitle = internalModel.name;
    NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
    YSFormCommonCell *cell0 = [self.tableView cellForRowAtIndexPath:indexPath0];
    [cell0 updateData:model0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath0] withRowAnimation:UITableViewRowAnimationAutomatic];
    //公司
    YSFormRowModel *model1 = _configArray[0][1];
    model1.detailTitle = internalModel.companyName;
    NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
    YSFormCommonCell *cell1 = [self.tableView cellForRowAtIndexPath:indexPath1];
    [cell1 updateData:model1];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath1] withRowAnimation:UITableViewRowAnimationAutomatic];
    //更新职务
    //职务不需要从列表选择了，从选择的工号去请求回来
    [YSNetManager ys_request_GETWithUrlString:[NSString stringWithFormat:@"%@%@/%@",YSDomain,getPostNameByNo,internalModel.userId] isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"======%@",response);
        if ([response[@"code"] integerValue]) {
            YSFormRowModel *model3 = _configArray[0][3];
            model3.detailTitle = response[@"data"][@"postName"];
            NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0];
            YSFormCommonCell *cell3 = [self.tableView cellForRowAtIndexPath:indexPath3];
            [cell3 updateData:model3];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath3] withRowAnimation:UITableViewRowAnimationAutomatic];
           //配置参数
            [self.payload setValue:response[@"data"][@"postCode"] forKey:@"jobLevelId"];
            [self.payload setValue:response[@"data"][@"postName"] forKey:@"jobLevelName"];
            
        }
    } failureBlock:^(NSError *error) {
        DLog(@"=======%@",error);
        
    } progress:nil];
}
#pragma mark - 添加行程通知
- (void)addTrip:(NSNotification *)notification {

    NSDictionary *trip = notification.userInfo[@"trip"];
    DLog(@"trip========%@", trip);
    //遍历将行程的区变为不可编辑
    for (int i = 0; i < self.configArray.count; i++) {
        if (i == 0) {//出差
            
        }else{
            for (YSFormRowModel *model in self.configArray[i]) {
                model.disable = YES;
                model.accessoryType = UITableViewCellAccessoryNone;
            }
        }
    }
    
    
    if ([trip[@"isEditing"] boolValue]) {//编辑 更新区
        NSIndexPath *indexPath = trip[@"indexPath"];
//         [self.tableView beginUpdates];
//        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
//        [self.tableView endUpdates];
        [self.tableView reloadData];
        //跟新上传参数
        [self.tripListArray replaceObjectAtIndex:indexPath.section - 1 withObject:trip[@"payload"]];
        [self.buyTicketsArray replaceObjectAtIndex:indexPath.section - 1 withObject:trip[@"buyTickets"]];
        
    }else {//添加 插入区
//        NSInteger index =  self.configArray.count - 1;
//        [self.tableView beginUpdates];
//        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationFade];
//        [self.tableView endUpdates];
        [self.tableView reloadData];
        [self.tripListArray addObject:trip[@"payload"]];//数组里一个行程的字典
        [self.buyTicketsArray addObject:trip[@"buyTickets"]];
    }

    [self checkNeedIdCard:self.buyTicketsArray];
}

- (void)checkNeedIdCard:(NSArray *)buyTicketsArray {
    YSWeak;
    weakSelf.needIdCard = NO;
    for (NSString *value in buyTicketsArray) {
        YSStrong;
        if ([value integerValue] == 1) {
            strongSelf.needIdCard = YES;
        }
    }
   
    YSFormRowModel *model6 = weakSelf.configArray[0][6];
    model6.detailTitle = weakSelf.payload[@"idCard"];
    model6.necessary = weakSelf.needIdCard;
    [weakSelf.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:6 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - 提交数据
- (void)submit {
//    if (self.payload[@"businessPno"] || self.payload[@"remark"] || self.payload[@"jobLevelName"] || self.payload[@"startTime"]  || self.payload[@"endTime"]) {
//        [QMUITips showError:@"请填写完整" inView:self.view hideAfterDelay:1];
//        return;
//    }
    
    if (_needIdCard) {//选择了交通代买，身份证必要
        if ([self.payload[@"idCard"] isEqual:@""]) {
            [QMUITips showError:@"请填写完整" inView:self.view hideAfterDelay:1];
            return;
        }
    }
    
    if (self.tripListArray.count == 0) {
        [QMUITips showError:@"请添加至少一个行程" inView:self.view hideAfterDelay:1];
        return;
    }
    
    //检测出差安排时间是不是和行程时间吻合
    NSDate *startDate = [NSDate dateWithString:self.payload[@"startTime"] formatString:@"yyyy-MM-dd"];
    NSDate *endDate = [NSDate dateWithString:self.payload[@"endTime"] formatString:@"yyyy-MM-dd"];
    for (int i = 0; i <  [self.payload[@"businessTripList"] count] ; i ++) {
        NSString *dateStr = self.payload[@"businessTripList"][i][@"startTime"];
        NSDate *date = [NSDate dateWithString:dateStr formatString:@"yyyy-MM-dd"];

        if (startDate.timeIntervalSince1970 > date.timeIntervalSince1970 || endDate.timeIntervalSince1970 < date.timeIntervalSince1970) {
            [QMUITips showInfo:@"出差时间和行程时间不符" inView:self.view hideAfterDelay:2];
            return;
        }
        
    }
    //检测是不是有历史出差重合
    [YSNetManager ys_request_GETWithUrlString:[NSString stringWithFormat:@"%@%@/%@/%@/%@",YSDomain,applyCheckTripApi,self.payload[@"businessPno"],self.payload[@"startTime"],self.payload[@"endTime"]] isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"----------%@",response);
        if ([response[@"code"] integerValue] == 1) {
            if ([response[@"data"] count]) {//有重复
                QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"是" style:(QMUIAlertActionStyleDestructive) handler:^(QMUIAlertAction *action) {
                    [self summitRequest];//提交
                }];
                QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"否" style:(QMUIAlertActionStyleCancel) handler:^(QMUIAlertAction *action) {
                    
                }];
                NSMutableString *alertStr = [[NSMutableString alloc]init];
                for (int i = 0; i < [response[@"data"] count]; i++) {
                    if (i == 0) {
                        [alertStr appendString:response[@"data"][i]];
                    }else{
                        [alertStr appendString:[NSString stringWithFormat:@"、%@",response[@"data"][i]]];
                    }
                }
                QMUIAlertController *alertVC = [[QMUIAlertController alloc]initWithTitle:nil message:[NSString stringWithFormat:@"您已提交过该时间范围内的出差单(%@),是否仍要提交？",alertStr] preferredStyle:(QMUIAlertControllerStyleAlert)];
                [alertVC addAction:action1];
                [alertVC addAction:action2];
                [alertVC showWithAnimated:YES];
            }else {
                [self summitRequest];//提交
            }
        }
    } failureBlock:^(NSError *error) {

    } progress:nil];

  
}
- (void)summitRequest {
    DLog(@"=======%@",self.payload);
    //提交
    if (_needIdCard) {
        if ([self.payload[@"idCard"] isEqual:@""]) {
            [QMUITips showError:@"请填写完整" inView:self.view hideAfterDelay:1];
            return;
        }
        self.navigationItem.rightBarButtonItem.enabled = NO;
        [QMUITips showLoadingInView:self.view];
        NSString *urlString = [NSString stringWithFormat:@"%@%@", YSDomain, applyTripApi];
        [YSNetManager ys_request_POSTWithUrlString:urlString isNeedCache:NO parameters:self.payload successBlock:^(id response) {
            [QMUITips hideAllTipsInView:self.view];
            DLog(@"payload:%@", self.payload);
            DLog(@"申请出差结果:%@", response);
            if ([response[@"code"] intValue] == 1) {
                [QMUITips showInfo:@"该出差申请流程已成功提交！" inView:self.view hideAfterDelay:2];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.navigationItem.rightBarButtonItem.enabled = YES;
                     [self.navigationController popViewControllerAnimated:YES];
                });
               
            }else{
               self.navigationItem.rightBarButtonItem.enabled = YES;
            }
        } failureBlock:^(NSError *error) {
            DLog(@"error:%@", error);
            self.navigationItem.rightBarButtonItem.enabled = YES;
        } progress:nil];
    } else {
        [QMUITips showLoadingInView:self.view];
        self.navigationItem.rightBarButtonItem.enabled = NO;
        NSString *urlString = [NSString stringWithFormat:@"%@%@", YSDomain, applyTripApi];
        [YSNetManager ys_request_POSTWithUrlString:urlString isNeedCache:NO parameters:self.payload successBlock:^(id response) {
            DLog(@"payload:%@", self.payload);
            DLog(@"申请出差结果:%@", response);
            [QMUITips hideAllTipsInView:self.view];
            self.navigationItem.rightBarButtonItem.enabled = YES;
            if ([response[@"code"] intValue] == 1) {
                [QMUITips showInfo:@"该出差申请流程已成功提交！" inView:self.view hideAfterDelay:1.5];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.navigationItem.rightBarButtonItem.enabled = YES;
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                self.navigationItem.rightBarButtonItem.enabled = YES;
            }
        } failureBlock:^(NSError *error) {
            DLog(@"error:%@", error);
            self.navigationItem.rightBarButtonItem.enabled = YES;
        } progress:nil];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
